from __future__ import annotations

from datetime import date
import os

from quantcore.config import CONFIG
from quantcore.db import Mongo
from quantcore.data import StrongStockService
from quantcore.alpha import MainlineStrong
from quantcore.portfolio import OrderGenerator
from quantcore.cost import CostModel
from quantcore.execution import BrokerAPI, Executor


def run_daily(trade_date: date) -> None:
    skip_db = os.getenv("SKIP_DB", "0") == "1"

    if not skip_db:
        # 计算并保存强势股（可通过 SKIP_DB 跳过数据库操作）
        StrongStockService().compute_and_save(trade_date)

    picks = [{"symbol": "000001", "price": 10.0, "qty": 100, "side": "BUY"}]
    selected = MainlineStrong().pick([p["symbol"] for p in picks])
    picks = [p for p in picks if p["symbol"] in selected]

    if skip_db:
        # 演示运行：不访问数据库，仅打印结果
        print(f"[DRY-RUN] {trade_date} picks: {picks}")
        return

    orders = OrderGenerator(CostModel()).generate(picks)
    executed = Executor(BrokerAPI()).execute_orders(orders)
    print(f"已在 {trade_date} 执行 {len(executed)} 笔委托")


if __name__ == "__main__":
    run_daily(date.today())




